package com.niit.white_box.common;

import lombok.Data;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;


@Data
public class Result {
    private Boolean success;
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();


    public static Result success(com.niit.white_box.entity.User user) {
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("success");
        result.data("user", user);
        return result;
    }

    public static Result fail(String message) {
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(500);
        result.setMessage(message);
        return result;
    }

    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }
    public static Result success(String message) {
        Result result = new Result();
        result.setSuccess(true);
        result.setMessage(message);
        return result;
    }

    // 新增：通用 success(data) 重载，适配分页等任意数据结构
    public static Result success(Object dataObj) {
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("success");
        if (dataObj instanceof java.util.Map) {
            // 直接将 Map 内容作为 data 返回，避免出现 data.data 的多一层嵌套
            @SuppressWarnings("unchecked")
            java.util.Map<String, Object> m = (java.util.Map<String, Object>) dataObj;
            result.getData().putAll(m);
        } else {
            result.data("data", dataObj);
        }
        return result;
    }

}